﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aurora.WebApp.Helpers;
using Aurora.WebApp.ViewModel.Home;
using Aurora.WebApp.Filters;
using PagedList;

namespace Aurora.WebApp.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ContentResult KeepAlive()
        {
            Session.Set(SessionKey.UsuarioId, Session.Get(SessionKey.UsuarioId));
            return Content("");
        }

        [AppAuthorize(AppRol.Administrador)]
        [ViewParameter(Section: "Dashboard", PageIcon: "fa fa-dashboard")]
        public ActionResult AdministradorIndex()
        {
            return View();
        }

        [AppAuthorize(AppRol.Administrador)]
        [ViewParameter(Section: "Dashboard", PageIcon: "fa fa-dashboard")]
        public ActionResult TrabajadorIndex()
        {
            return View();
        }
        public ActionResult Dashboard()
        {
            switch (Session.GetRol())
            {
                case AppRol.Trabajador: return RedirectToAction("TrabajadorIndex");
                case AppRol.Administrador: return RedirectToAction("AdministradorIndex");
            }

            return RedirectToAction("Login");
        }
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel modelo)
        {
            if (!ModelState.IsValid)
            {
                return View(modelo);
            }

            var usuario = context.Trabajador.FirstOrDefault(x => x.TRA_CODIGO == modelo.Codigo);

            if (usuario == null || String.IsNullOrEmpty(usuario.TRA_CONTRASENA) || modelo.Contrasena != usuario.TRA_CONTRASENA)
            {
                PostMessage(MessageType.Error, "Usuario y/o Contraseña Incorrectos");
            }
            else
            {
                Session.Clear();
                AppRol rol = new AppRol();

                switch (usuario.Rol.ROL_CODIGO)
                {
                    case ConstantHelpers.ROL_ADMINISTRADOR: rol = AppRol.Administrador; break;
                    case ConstantHelpers.ROL_USUARIO: rol = AppRol.Trabajador; break;
                }

                Session.Set(SessionKey.Usuario, usuario);
                Session.Set(SessionKey.UsuarioId, usuario.TRA_ID);
                //Session.Set(SessionKey.Email, usuario.tra);
                Session.Set(SessionKey.NombreCompleto, usuario.TRA_NOMBRE);
                Session.Set(SessionKey.Rol, rol);
                Session.Set(SessionKey.RolCompleto, usuario.Rol);

                return Dashboard();
            }

            return View(modelo);
        }

        public ActionResult Logout()
        {
            Session.Clear();
            return RedirectToAction("Login");
        }
    }
}
